package util;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class EmailSender {
	private static final String SMTP_HOST = "smtp.gmail.com";
	private static final int SMTP_PORT = 587;
	
	//administrator's email info
	private static final String from = "m2p.gl.2010@gmail.com";
	private static final String password = "m2pgl2010";	
	private static final String recipient = "m2p.gl.2010@gmail.com";
	
	//sender's info
	private String firstName = "";
	private String lastName = "";
	private String email = "";
	
	public EmailSender() {}		
	
	public EmailSender(String firstName, String lastName, String email) {
		super();
		this.firstName = firstName;
		this.lastName = lastName;
		this.email = email;
	}

	public void setFirstnameSender(String firstnameSender) {
		this.firstName = firstnameSender;
	}

	public void setLastnameSender(String lastnameSender) {
		this.lastName = lastnameSender;
	}

	public void setEmailSender(String emailSender) {
		this.email = emailSender;
	}


	public void send(String subject, String text) throws MessagingException {
		Properties props = System.getProperties();
	    props.put("mail.smtp.starttls.enable", "true");
	    props.put("mail.smtp.host", SMTP_HOST);
	    props.put("mail.smtp.user", from);
	    props.put("mail.smtp.password", password);
	    props.put("mail.smtp.port", SMTP_PORT);
	    props.put("mail.smtp.auth", "true");

	    String[] to = {recipient}; //recipient email array

	    Session session = Session.getDefaultInstance(props, null);
	    MimeMessage message = new MimeMessage(session);
	    message.setFrom(new InternetAddress(from));

	    InternetAddress[] toAddress = new InternetAddress[to.length];

	    //get array of addresses
	    for( int i=0; i < to.length; i++ ) {
	        toAddress[i] = new InternetAddress(to[i]);
	    }
	    System.out.println(Message.RecipientType.TO);

	    for( int i=0; i < toAddress.length; i++) {
	        message.addRecipient(Message.RecipientType.TO, toAddress[i]);
	    }
	    message.setSubject(subject);
	    message.setText(text + getSenderInfo());
	    Transport transport = session.getTransport("smtp");
	    transport.connect(SMTP_HOST, from, password);
	    transport.sendMessage(message, message.getAllRecipients());
	    transport.close();
	}
	
	private String getSenderInfo() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("\n\n=================\n");
		buffer.append("Sender's information:\n\n");
		buffer.append(firstName + " " + lastName.toUpperCase() + "\n");
		buffer.append(email);
		return buffer.toString();
	}
	
//	//test
//	public static void main(String[] args) throws AddressException, MessagingException {
//		EmailSender mailSender = new EmailSender();
//		mailSender.setFirstnameSender("Chinh");
//		mailSender.setLastnameSender("HO");
//		mailSender.setEmailSender("chinh.rox@gmail.com");
//		System.out.println("sending");
//		mailSender.send("subjecttt", "texxxxxxxxxxxxt");
//		System.out.println("The mail is successfully sent!");
//	}
}
